import type { AutoFixResult } from "../types/autoFix";

type Props = {
  result: AutoFixResult | null;
};

export function AutoFixPreview({ result }: Props) {
  if (!result) return null;

  const hasFiles = (result.fixed_files?.length ?? 0) > 0;
  const hasChat = (result.chat_history?.length ?? 0) > 0;

  return (
    <div className="mt-6">
      <h2 className="text-lg font-semibold mb-2">自动修复预览：</h2>

      {!result.success && (
        <div className="p-3 bg-red-50 border border-red-200 text-red-700 rounded">
          自动修复失败：{result.error || "未知错误"}
        </div>
      )}

      {hasChat && (
        <div className="mb-4 border border-gray-200 rounded overflow-hidden">
          <div className="bg-gray-100 px-4 py-2 text-sm font-medium">对话记录</div>
          <div className="p-3 max-h-60 overflow-y-auto text-sm space-y-2">
            {result.chat_history!.map((m, i) => (
              <div key={i} className="flex gap-2">
                <span className="text-gray-500 min-w-20 text-right capitalize">{m.role}:</span>
                <pre className="whitespace-pre-wrap text-gray-800 flex-1">{m.content}</pre>
              </div>
            ))}
          </div>
        </div>
      )}

      {hasFiles ? (
        <div className="border border-gray-200 rounded overflow-hidden">
          <div className="bg-gray-100 px-4 py-2 text-sm font-medium">生成的修复文件</div>
          <div className="divide-y divide-gray-100">
            {result.fixed_files!.map((f, idx) => (
              <details key={idx} className="p-3">
                <summary className="cursor-pointer text-sm font-mono text-gray-800">
                  {f.name} <span className="text-gray-400">({f.path})</span>
                </summary>
                <pre className="mt-2 bg-gray-900 text-green-200 p-3 rounded text-xs overflow-x-auto whitespace-pre-wrap">{f.content}</pre>
              </details>
            ))}
          </div>
        </div>
      ) : (
        <div className="p-3 bg-yellow-50 border border-yellow-200 text-yellow-700 rounded text-sm">
          暂无修复文件输出，查看上方对话记录获取修复建议。
        </div>
      )}
    </div>
  );
}
